<template>
	<el-dialog v-model="isVisible" title="协助买家退货" width="600px" :before-close="handleClose">
		<el-form ref="formRef" class="address-form" :model="form" :rules="rules" label-width="100px" inline>
			<el-form-item label="快递公司" prop="expressName">
				<el-select v-model="form.expressName" placeholder="选择快递公司">
					<el-option v-for="item in constant.EXPRESSAG" :key="item.name" :label="item.name" :value="item.name" />
				</el-select>
			</el-form-item>
			<el-form-item label="快递单号" prop="expressNo">
				<el-input v-model="form.expressNo" @input="noInput" placeholder="输入快递单号" />
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch } from 'vue';
import { fetchHelpAfterSale } from '/@/api/order';
import constant from '/@/utils/constant';

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({});

//校验规则
const rules = reactive<FormRules<any>>({
	expressName: [{ required: true, trigger: 'blur', message: '快递公司不能为空' }],
	expressNo: [{ required: true, trigger: 'blur', message: '快递单号不能为空' }],
});

watch(
	() => props.visible,
	(val) => {
		isVisible.value = val;
	},
	{
		immediate: true,
	}
);

const noInput = (val: any) => {
	form.value.no = val.replace(/[^a-zA-Z0-9]/g, '');
};

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			const res = await fetchHelpAfterSale({ ...form.value, id: props.row?.afterMarketing?.id || props.row?.id });
			if (res.code == 200) {
				ElMessage.success('操作成功');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	form.value = {};
	formRef.value.resetFields();
	emit('handleClose', success === true);
};
</script>

<style lang="scss" scoped>
.address-form {
	.el-form-item {
		width: 90%;
		.el-input-number,
		.el-cascader {
			width: 100%;
		}
	}
}
</style>
